package com.lanju.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import com.lanju.entity.User;
import com.lanju.service.JedisService;
@Service
public class JedisServiceImpl implements JedisService {
	
	@Resource(name="jedisPool")
	private  JedisPool jedisPool;
	
	private final Map<String,Object> resultMap=new HashMap<String, Object>();
	
	public JedisServiceImpl() {
		
	}

	@Override
	public Map<String, Object> get(String key) {
		Jedis jedis = null;
		resultMap.clear();
		try {
			jedis = jedisPool.getResource();
			String string=jedis.get(key);
			resultMap.put("value", string);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			jedis.close();
		}
		return resultMap;
	}
	@Override
	public Map<String,Object> setMap(String key,Map<String,String> map,int seconds){
		Jedis jedis = null;
		resultMap.clear();
		try {
			jedis = jedisPool.getResource();
			jedis.hmset(key, map);
			jedis.expire(key, seconds);
			System.out.println("key的长度是："+jedis.llen(key));
			resultMap.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("success", false);
		}finally{
			jedis.close();
		}
		return resultMap;
	}
	
	@Override
	public Map<String, Object> getMap(String key,String... field) {
		Jedis jedis = null;
		resultMap.clear();
		try {
			jedis = jedisPool.getResource();
			List<String> list=jedis.hmget(key, field);
			System.out.println("获取的list是："+list);
			resultMap.put("rows", list);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			jedis.close();
		}
		return resultMap;
	}
	
	@Override
	public Boolean hexists(String key, String field) {
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			return jedis.hexists(key, field);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			jedis.close();
		}
		return null;
	}

	@Override
	public User getUser(String key, String... field) {
		Jedis jedis = null;
		resultMap.clear();
		User userModel=null;
		try {
			
			jedis = jedisPool.getResource();
			List<String> list=jedis.hmget(key, field);
			if(list!=null && list.size()>0){
				userModel=new User();
				userModel.setHeadImg(list.get(2));
				userModel.setFlag(Integer.valueOf(list.get(8).toString()));
				userModel.setNickName(list.get(0));
				userModel.setUserId(list.get(1));
				userModel.setOpenId(list.get(3));
				userModel.setSex(Integer.valueOf(list.get(6)));
				userModel.setStatus(Integer.valueOf(list.get(7)));
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			jedis.close();
		}
		return userModel;
	}

	@Override
	public Long delKey(String key) {
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			return  jedis.del(key);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}finally{
			jedis.close();
		}
	}

	@Override
	public String setValue(String key, String val, int seconds) {
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			return jedis.setex(key, seconds, val);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}finally{
			jedis.close();
		}
	}

	@Override
	public String getVal(String key) {
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			jedis.close();
			return jedis.get(key);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}finally{
			jedis.close();
		}
	}

}
